package com.cssl.utils;

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: chen
 * @Date: 2021/11/18 22:22
 * @Desc: 分词工具类
 */
public class KeywordUtil {
    public static List<String> splitKeyword(String keyword) throws IOException {

        //创建集合用于存放结果数据
        ArrayList<String> words = new ArrayList<>();

        StringReader reader = new StringReader(keyword);

        IKSegmenter ikSegmenter = new IKSegmenter(reader, false);

        while (true) {
            Lexeme next = ikSegmenter.next();
            if (next != null) {
                String word = next.getLexemeText();
                words.add(word);
            } else {
                break;
            }
        }
        return words;
    }
}
